package promote.brozen.base.io.nio;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.charset.CoderResult;

/**
 * @author Brozen
 * @date 2020/10/9 9:01 AM
 * @email brozen@qq.com
 */
public class RCRead {

    public static void main(String[] args) throws IOException {

        RandomAccessFile raf = new RandomAccessFile("/data/logs/error.log", "r");
        FileChannel fc = raf.getChannel();

        ByteBuffer buffer = ByteBuffer.allocate((int) fc.size());
        CharBuffer cbuffer = CharBuffer.allocate((int) fc.size());

        while (fc.read(buffer) != 0) ;

        buffer.flip();
        CoderResult result = Charset.forName("UTF-8").newDecoder().decode(buffer, cbuffer, false);

        cbuffer.flip();
        System.out.println(cbuffer.get());

    }

}
